﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BingZi.HY.Core;
using System.Collections;
using NewLife.Web;
using BingZi;

namespace FrameWork.web
{
    public partial class Buy : BaseTemplatePage
    {

        #region "==属性=="

        private Int32 ProductID = WebHelper.GetIntParam(HttpContext.Current.Request, "id", 0);
        /// <summary>
        /// 商品
        /// </summary>
        private app_Product ProductItem
        {
            get{return app_Product.FindByKeyForEdit(ProductID); }
        }
        /// <summary>
        /// 请求动作
        /// </summary>
        private String Action = WebHelper.GetStringParam(HttpContext.Current.Request, "action", "");
       

        #endregion

        #region "==方法=="

        /// <summary>
        /// 绑定页面元素
        /// </summary>

        /// <summary>
        /// 绑定页面元素
        /// </summary>
        private void BindPageItem()
        {
            MessageBox MBx = new MessageBox();
            MBx.M_Type = 1;
         
          
            if (ProductID > 0)
            {
                app_Product Product = ProductItem;
                if (Product != null && Product.ID > 0)
                {
                    if (Product.Status == (Int32)EnumProductStatus.Published)
                    {

                        Hashtable puts = new Hashtable();
                        puts.Add("Product", Product);

                        Response.Write(ViewTemplate(EnumTemplateFile.Buy, puts));
                        return;
                    }
                    else
                    {
                        //商品未上架或者已经删除了
                        MBx.M_Title = "商品未上架或者已经删除了！";
                        MBx.M_Body = "商品未上架或者已经删除了,请重新选择。";
                   
                        
                    }
                }
                else
                {
                    //商品未找到
                    MBx.M_Title = "商品未上架或者已经删除了！";
                    MBx.M_Body = "商品未上架或者已经删除了,请重新选择。";
                }
            }
            else
            {
                //商品未找到
                MBx.M_Title = "商品未上架或者已经删除了！";
                MBx.M_Body = "商品未上架或者已经删除了,请重新选择。";
            }
            MBx.M_IsView = true;
            MBx.M_IconType = Icon_Type.Error;
            MBx.M_WriteToDB = false;
            MBx.M_ButtonList.Add(new sys_NavigationUrl("返回", "index.aspx", "点击按钮返回首页！", UrlType.Href, true));
            EventMessage.MessageBox(MBx);
        }

        /// <summary>
        /// 购买商品
        /// </summary>
        private void BindBuy()
        {

            MessageBox MBx = new MessageBox();
            MBx.M_Type = 1;
            MBx.M_IconType = Icon_Type.Alert;
            sys_NavigationUrl Navigation = new sys_NavigationUrl("返回", "index.aspx", "点击按钮返回首页！", UrlType.Href, true);
            
            if (ProductID > 0)
            {
                app_Product Product = ProductItem;
                if (Product != null && Product.ID > 0)
                {
                    if (Product.Status == (Int32)EnumProductStatus.Published )
                    {
                      
                        if (Product.InventoryNumber > 0)
                        {
                            app_Order Order = new app_Order();
                            
                            Order.ProductID = Product.ID;
 
                            Order.BuyCount = WebHelper.GetIntParam(Request, "txtBuyCount", 1);
 
                            if ((Product.InventoryNumber - Order.BuyCount) >= 0)
                            {
 
                                //接受参数
                                Order.Addressee = WebHelper.GetStringParam(Request, "txtAddressee", "");
                                Order.Address = WebHelper.GetStringParam(Request, "txtAddress", "");
                                Order.Phone = WebHelper.GetStringParam(Request, "txtPhone", "");
                                Order.ZipCode = WebHelper.GetStringParam(Request, "txtZipCode", "");
                                Order.Remark = WebHelper.GetStringParam(Request, "txtRemark", "");


                                Order.BuyUserID = Common.Get_UserID;
                                Order.BuyTime = DateTime.Now;
                                Order.BuyIP = WebHelper.UserHost;

                                Order.LastIP = WebHelper.UserHost;
                                Order.LastTime = DateTime.Now;
                                Order.LastUser = Common.Get_UserID;

                                Order.Status = (Int32)EnumOrderStatus.Unaudited;
                                if (Order.Insert() > 0)
                                {
                                    //下单成功
                                    Product.InventoryNumber -= Order.BuyCount; //扣库存
                                    Product.SalesNumber += Order.BuyCount;      //加销量
                                    Product.Update();

                                    MBx.M_Title = "你的商品购买成功！";
                                    MBx.M_Body = "你的商品购买成功,请等待客服的通知。";
                                    MBx.M_IconType = Icon_Type.OK;
                                    Navigation = new sys_NavigationUrl("返回", String.Format("/team.aspx?id={0}", Product.ID), "点击按钮返回商品页！", UrlType.Href, true);
                                }
                            }
                            else
                            {
                                //你购买的数量超限
                                MBx.M_Title = "你购买的数量超过现有库存！";
                                MBx.M_Body = "你购买的数量超过现有库存,请重新选择。";
                                Navigation = new sys_NavigationUrl("返回", String.Format("/team.aspx?id={0}", Product.ID), "点击按钮返回商品页！", UrlType.Href, true);
                            }
                        }
                        else
                        {
                            //库存数量不够
                            MBx.M_Title = "商品库存数量不够！";
                            MBx.M_Body = "商品库存数量不够,请重新选择。";
                            Navigation = new sys_NavigationUrl("返回", String.Format("/team.aspx?id={0}", Product.ID), "点击按钮返回商品页！", UrlType.Href, true);
                        }
                    }
                    else
                    {
                        //商品未上架或者已经删除了
                        MBx.M_Title = "商品未上架或者已经删除了！";
                        MBx.M_Body = "商品未上架或者已经删除了,请重新选择。";
                    }
                }
                else
                {
                    //商品未找到
                    MBx.M_Title = "商品未上架或者已经删除了！";
                    MBx.M_Body = "商品未上架或者已经删除了,请重新选择。";
                }
            }
            else
            {
                //商品未找到
                MBx.M_Title = "商品未上架或者已经删除了！";
                MBx.M_Body = "商品未上架或者已经删除了,请重新选择。";
            }
            MBx.M_IsView = true;
            MBx.M_WriteToDB = false;
           MBx.M_ButtonList.Add(Navigation);
            EventMessage.MessageBox(MBx);


        }

        #endregion

        #region "==事件=="


        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (!IsPostBack)
                {
                    if (!String.IsNullOrEmpty(Action) && Action.ToLower() == "buy")
                    {
                        //if (Common.Get_UserID > 0)
                        //{
                            
                            BindBuy();
                        //}
                        //else
                        //{
                            //Response.Redirect(String.Format("/LoginAndReg.aspx?ReturnUrl={0}",HttpUtility.UrlEncode(Common.GetScriptUrl)));
                        //}
                    }
                    else
                    {

                        //绑定页面元素
                        BindPageItem();
                    }

                }
            }
            catch (Exception ex)
            {

            }

        }




        #endregion





    }
}